package com.bop.dehong.util;

import java.util.HashMap;

import android.app.Activity;
import android.os.Handler.Callback;
import android.os.Message;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.PopupWindow;
import android.widget.Toast;
import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.Platform.ShareParams;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.sina.weibo.SinaWeibo;
import cn.sharesdk.system.text.ShortMessage;
import cn.sharesdk.tencent.qq.QQ;
import cn.sharesdk.tencent.qzone.QZone;
import cn.sharesdk.wechat.friends.Wechat;
import cn.sharesdk.wechat.moments.WechatMoments;

import com.bop.dehong.R;
import com.mob.tools.utils.UIHandler;


public class SharePopUtil implements OnClickListener, PlatformActionListener,
		Callback {
	private Activity mActivity;
	private View mParent;
	private PopupWindow mPopupWindow;
	private String defaultTitle = "分享的标题";
	private String defaultContent = "分享的内容";
	private String defaultImage = "http://f1.sharesdk.cn/imgs/2014/05/21/oESpJ78_533x800.jpg";
	private String defaultUrl = "http://www.baidu.com";
	private int flag;
	public static int shareSDImg = 0;
	public static int shareNetImg = 1;

	public SharePopUtil(Activity mActivity, View mParent) {
		this.mActivity = mActivity;
		this.mParent = mParent;
		ShareSDK.initSDK(mActivity);
		initView();
	}

	/**
	 * 
	 * @param title
	 *            分享的标题
	 * @param imgUrl
	 *            分享的图片LOGO
	 * @param content
	 *            分享的内容
	 * @param mUrl
	 *            分享点击路径
	 */
	public void setShareParams(String title, String imgUrl, String content,
			String mUrl, int flag) {
		this.defaultTitle = title;
		this.defaultContent = content;
		this.defaultImage = imgUrl;
		this.defaultUrl = mUrl;
		this.flag = flag;
	}

	private void initView() {
		LayoutInflater mLayoutInflater = LayoutInflater.from(mActivity);
		View mView = mLayoutInflater.inflate(R.layout.share_layout, null);
		mPopupWindow = new PopupWindow(mView, LayoutParams.MATCH_PARENT,
				LayoutParams.MATCH_PARENT);
		final LinearLayout mLayout = (LinearLayout) mView
				.findViewById(R.id.shares_popup_window);
		mView.findViewById(R.id.share_sinaweibo).setOnClickListener(this);
		mView.findViewById(R.id.share_wechat).setOnClickListener(this);
		mView.findViewById(R.id.share_wechatmoments).setOnClickListener(this);
		mView.findViewById(R.id.share_qq).setOnClickListener(this);
		mView.findViewById(R.id.share_qzone).setOnClickListener(this);
		mView.findViewById(R.id.share_shortmessage).setOnClickListener(this);
		mView.findViewById(R.id.shares_cancel).setOnClickListener(this);
		mPopupWindow.setFocusable(true);
		mPopupWindow.setOutsideTouchable(true);
		mPopupWindow.setTouchInterceptor(new OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				int top = mLayout.getTop();
				int bottom = mLayout.getBottom();
				int left = mLayout.getLeft();
				int right = mLayout.getRight();
				int x = (int) event.getX();
				int y = (int) event.getY();
				if (event.getAction() == MotionEvent.ACTION_UP) {
					if (x < left || x > right) {
						mPopupWindow.dismiss();
					}
					if (y > bottom || y < top) {
						mPopupWindow.dismiss();
					}
				}
				return false;
			}
		});
		mPopupWindow.showAtLocation(mParent, Gravity.CENTER, 0, 0);
	}

	@Override
	public void onClick(View v) {
			switch (v.getId()) {
			case R.id.share_sinaweibo:
				share(SinaWeibo.NAME);
				break;
			case R.id.share_wechat:
				share(Wechat.NAME);
				break;
			case R.id.share_wechatmoments:
				share(WechatMoments.NAME);
				break;
			case R.id.share_qq:
				share(QQ.NAME);
				break;
			case R.id.share_qzone:
				share(QZone.NAME);
				break;
			case R.id.share_shortmessage:
				share(ShortMessage.NAME);
				break;
			case R.id.shares_cancel:
				break;
			}
			mPopupWindow.dismiss();
		}

	private void share(String shareName) {
		// TODO Auto-generated method stub
		Platform platform = ShareSDK.getPlatform(shareName);
		ShareParams sp = new ShareParams();
		if (shareName.equals(SinaWeibo.NAME)) {
			defaultContent = defaultTitle +"。"+ defaultContent+defaultUrl;
			sp.setText(defaultContent);
			if (flag == shareNetImg) {
				sp.setImageUrl(defaultImage);
			} else if (flag == shareSDImg) {
				sp.setImagePath(defaultImage);
			}
 		} else if(shareName.equals(Wechat.NAME)||shareName.equals(WechatMoments.NAME)){
			sp.setTitle(defaultTitle);
			sp.setText(defaultContent);
			if (flag == shareNetImg) {
				sp.setImageUrl(defaultImage);
			} else if (flag == shareSDImg) {
				sp.setImagePath(defaultImage);
			}
			sp.setUrl(defaultUrl);
			sp.setShareType(Platform.SHARE_WEBPAGE);
		}else if(shareName.equals(QQ.NAME)){
			sp.setTitle(defaultTitle);
			sp.setText(defaultContent);
			if (flag == shareNetImg) {
				sp.setImageUrl(defaultImage);
			} else if (flag == shareSDImg) {
				sp.setImagePath(defaultImage);
			}
			sp.setTitleUrl(defaultUrl);
		}else if(shareName.equals(QZone.NAME)){
			sp.setTitle(defaultTitle);
			sp.setText(defaultContent);
			if (flag == shareNetImg) {
				sp.setImageUrl(defaultImage);
			} else if (flag == shareSDImg) {
				sp.setImagePath(defaultImage);
			}
			sp.setTitleUrl(defaultUrl);
		}else if(shareName.equals(ShortMessage.NAME)){
			sp.setTitle(defaultTitle);
			sp.setText(defaultContent);
			sp.setAddress("");
			if (flag == shareNetImg) {
				sp.setImageUrl(defaultImage);
			} else if (flag == shareSDImg) {
				sp.setImagePath(defaultImage);
			}
			sp.setTitleUrl(defaultUrl);
		}
		platform.setPlatformActionListener(this);
		platform.share(sp);
	}

	@Override
	public void onCancel(Platform plat, int action) {
		// TODO Auto-generated method stub
		Message msg = new Message();
		msg.arg1 = 3;
		msg.arg2 = action;
		msg.obj = plat;
		UIHandler.sendMessage(msg, this);
	}

	@Override
	public void onComplete(Platform plat, int action,
			HashMap<String, Object> res) {
		// TODO Auto-generated method stub
		Message msg = new Message();
		msg.arg1 = 1;
		msg.arg2 = action;
		msg.obj = plat;
		UIHandler.sendMessage(msg, this);
	}

	@Override
	public void onError(Platform plat, int action, Throwable t) {
		// TODO Auto-generated method stub
		t.printStackTrace();
		Message msg = new Message();
		msg.arg1 = 2;
		msg.arg2 = action;
		msg.obj = t;
		UIHandler.sendMessage(msg, this);
	}

	public boolean handleMessage(Message msg) {
		String text = actionToString(msg.arg2);
		switch (msg.arg1) {
		case 1: {
			// 成功
			Platform plat = (Platform) msg.obj;
			String platName = "";
			 if (SinaWeibo.NAME.equals(plat.getName())){
				 platName = "新浪微博";
			 }else if (Wechat.NAME.equals(plat.getName())) {
				 platName = "微信";
			}else if (WechatMoments.NAME.equals(plat.getName())) {
				 platName = "朋友圈";
			}else if (QQ.NAME.equals(plat.getName())) {
				platName = "QQ";
			}else if (QZone.NAME.equals(plat.getName())) {
				platName = "QQ空间";
			}else if (ShortMessage.NAME.equals(plat.getName())) {
				platName = "短信";
			}
			text = "正在跳转到" + platName +  text;
//			Toast.makeText(mActivity, text, Toast.LENGTH_SHORT).show();
		}
			break;
		case 2: {
			// 失败
			if ("WechatClientNotExistException".equals(msg.obj.getClass()
					.getSimpleName())) {
				text = "目前您的微信版本过低或未安装微信，需要安装微信才能使用";
			} else if ("WechatTimelineNotSupportedException".equals(msg.obj
					.getClass().getSimpleName())) {
				text = "目前您的微信版本过低或未安装微信，需要安装微信才能使用";
			} else {
				text = "分享失败";
			}
			Toast.makeText(mActivity, text, Toast.LENGTH_SHORT).show();
		}
			break;
		case 3: {
			// 取消
			Platform plat = (Platform) msg.obj;
			String platName = "";
			 if (SinaWeibo.NAME.equals(plat.getName())){
				 platName = "新浪微博";
			 }else if (Wechat.NAME.equals(plat.getName())) {
				 platName = "微信";
			}else if (WechatMoments.NAME.equals(plat.getName())) {
				 platName = "朋友圈";
			}else if (QQ.NAME.equals(plat.getName())) {
				platName = "QQ";
			}else if (QZone.NAME.equals(plat.getName())) {
				platName = "QQ空间";
			}else if (ShortMessage.NAME.equals(plat.getName())) {
				platName = "短信";
			}
			text = "取消了" + platName + "分享";
			//Toast.makeText(mActivity, text, Toast.LENGTH_SHORT).show();
		}
			break;
		}		
		return true;
	}

	/** 将action转换为String */
	public String actionToString(int action) {
		switch (action) {
		case Platform.ACTION_AUTHORIZING:
			return "授权";
		case Platform.ACTION_GETTING_FRIEND_LIST:
			return "获取好友列表";
		case Platform.ACTION_FOLLOWING_USER:
			return "ACTION_FOLLOWING_USER";
		case Platform.ACTION_SENDING_DIRECT_MESSAGE:
			return "发送";
		case Platform.ACTION_TIMELINE:
			return "ACTION_TIMELINE";
		case Platform.ACTION_USER_INFOR:
			return "获取用户信息";
		case Platform.ACTION_SHARE:
			return "分享中";
		default: 
			return "UNKNOWN";
		}
	}
}
